package com.dailyblue.example.project.bookstore.user.handler;

import com.dailyblue.example.project.bookstore.commons.exception.NotLoginException;
import com.dailyblue.example.project.bookstore.commons.exception.RegisterException;
import com.dailyblue.example.project.bookstore.commons.exception.ShoppingCartIsEmptyException;
import com.dailyblue.example.project.bookstore.commons.util.JsonResult;
import com.dailyblue.example.project.bookstore.commons.util.ResultTool;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(NotLoginException.class)
    public JsonResult notLoginException(Exception e) {
        return ResultTool.fail(401, e.getMessage());
    }

    @ExceptionHandler(RegisterException.class)
    public JsonResult registerException(Exception e) {
        return ResultTool.fail(402, e.getMessage());
    }

    @ExceptionHandler(SQLException.class)
    public JsonResult sqlException(Exception e) {
        return ResultTool.fail(403, e.getMessage());
    }

    @ExceptionHandler(ShoppingCartIsEmptyException.class)
    public JsonResult emptyShoppingCartException(Exception e) {
        return ResultTool.fail(601, e.getMessage());
    }
}
